﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services.Protocols;
using ReportGenerator;
using System.IO;
using System.Web.Security;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Data;

/// <summary>
/// This code is used to generate reports.
/// </summary>
public class CVGenerator2
{
	public CVGenerator2()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static string CVMakeDir(string Username)
    {
        // Set network share path
        string ServerPath = "NetworkShare Path";

        // Create shared folder to save resumes
        string networkShare = ServerPath +"\\" + Username;

            using (new Tools.Impersonator("UserName", "Domain", "Password"))
            {
                if (!Directory.Exists(networkShare))
                {
                    Directory.CreateDirectory(networkShare);
                }
            }
        return networkShare;
    }

  
    public static void cvBatchResume(DataTable dtCreateResumes)
    {
        ReportExecutionService rs = new ReportExecutionService();
        rs.Credentials = new System.Net.NetworkCredential("Username", "Password", "Domain");
        rs.Url = @"http://reportservername/reportserver/reportexecution2005.asmx";
        //Set empty variables required for Render report method
        byte[] result = null;
        string reportPath;          
        string extension;           
        string format;              
        string encoding = String.Empty;
        string mimeType = String.Empty;
        Warning[] warnings = null;
        string[] streamIDs = null;
        string deviceInfo = null;
        string historyID = null;
        string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";

        using (new Tools.Impersonator("Username", "Domain", "Password"))
        {
            foreach (DataRow row in dtCreateResumes.Rows)
            {
                reportPath = row["ReportPath"].ToString();
                extension = row["Extension"].ToString();
                format = row["ReportFormat"].ToString();

                rs.LoadReport(reportPath, null);
                ParameterValue[] paramval = new ParameterValue[1];
                paramval[0] = new ParameterValue();
                paramval[0].Name = "EmployeeID";
                paramval[0].Value = row["ReportParam"].ToString();
                rs.SetExecutionParameters(paramval, "en-us");

                ExecutionInfo execInfo = new ExecutionInfo();
                ExecutionHeader execHeader = new ExecutionHeader();

                rs.ExecutionHeaderValue = execHeader;

                execInfo = rs.LoadReport(reportPath, historyID);

                rs.SetExecutionParameters(paramval, "en-us");
                try
                {
                    result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);

                    execInfo = rs.GetExecutionInfo();
                }
                catch (Exception ex)
                {

                }
                try
                {
                    using (FileStream stream = File.OpenWrite(row["OutputPath"].ToString() + row["SavedFile"].ToString()))
                    {
                        stream.Write(result, 0, result.Length);
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }
    }
}